package com.itaem.community.modules.infrastructure.company.department.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.core.entity.Entity;
import com.itaem.community.core.entity.PageData;
import com.itaem.community.core.exception.data.NoSuchDataException;
import com.itaem.community.core.utils.PageUtils;
import com.itaem.community.modules.infrastructure.company.company.entity.PropertyManagementCompanyEntity;
import com.itaem.community.modules.infrastructure.company.company.service.PropertyManagementCompanyService;
import com.itaem.community.modules.infrastructure.company.department.dto.*;
import com.itaem.community.modules.infrastructure.company.department.entity.DepartmentEntity;
import com.itaem.community.modules.infrastructure.company.department.mapper.DepartmentTransformer;
import com.itaem.community.modules.infrastructure.company.department.mapper.WorkerTransformer;
import com.itaem.community.modules.infrastructure.company.department.service.DepartmentService;
import com.itaem.community.modules.infrastructure.company.department.service.WorkerService;
import com.itaem.community.modules.infrastructure.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 物业部门 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "部门管理模块")
@ResponseResult
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private PropertyManagementCompanyService propertyManagementCompanyService;

    @Autowired
    private DepartmentTransformer departmentTransformer;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private WorkerService workerService;

    @Autowired
    private WorkerTransformer workerTransformer;

    @Autowired
    private UserService userService;

//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("分页查询部门信息")
    @GetMapping("/{page}/{size}")
    public PageData list(
            @ApiParam(value = "页码", example = "1") @PathVariable Long page,
            @ApiParam(value = "大小", example = "15") @PathVariable Long size){
        return PageUtils
                .extract(departmentService.page(new Page<>(page, size)))
                .map(departmentTransformer::entityToBriefDto);
    }

//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("获取指定部门的信息")
    @GetMapping("/{id}")
    public DepartmentDTO info(@ApiParam("部门id") @PathVariable("id") Integer departmentId){
        return departmentService.findDepartmentById(departmentId);
    }

//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("新增部门")
    @PostMapping
    public Boolean save(@Valid @RequestBody DepartmentCreateDTO createDto) {
        return departmentService.saveDepartment(createDto);
    }

//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("更新部门信息")
    @PatchMapping
    public Boolean update(@Valid @RequestBody DepartmentUpdateDTO updateDto) {
        if (updateDto.getCompanyId() != null && propertyManagementCompanyService.count(
                new QueryWrapper<PropertyManagementCompanyEntity>()
                        .lambda()
                        .eq(Entity::getId, updateDto.getCompanyId())
        ) == 0) {
            throw new NoSuchDataException("不存在指定的物业公司");
        }
        return departmentService.updateById(departmentTransformer.updateDtoToEntity(updateDto));
    }

//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("删除部门")
    @DeleteMapping("/{id}")
    public Boolean remove(@ApiParam("部门id") @PathVariable("id") Integer departmentId) {
        if (departmentService.count(new QueryWrapper<DepartmentEntity>().lambda().eq(Entity::getId, departmentId)) == 0) {
            return Boolean.TRUE;
        }
        return departmentService.removeDepartmentById(departmentId);
    }

//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("为部门新增员工")
    @PostMapping("/worker")
    public Boolean hireWorker(@Valid @RequestBody WorkerHireDTO hireDto) {
        return departmentService.addWorker(hireDto);
    }

//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("解雇部门员工")
    @DeleteMapping("/worker")
    public Boolean fireWorker(@Valid @RequestBody WorkerFireDTO fireDto) {
        return departmentService.removeWorker(fireDto);
    }

//    @RequiresRoles(value = {"超级管理员","物业工作人员"}, logical = Logical.OR)
    @ApiOperation("获取单个员工的信息")
    @GetMapping("/worker/{id}")
    public WorkerDTO getWorkerInfo(@ApiParam("员工id") @PathVariable("id") Integer workerId) {
        return departmentService.findWorker(workerId);
    }
}

